package util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * A crossover between the comfort of a {@link List} and the quick access to 
 * concrete items as provided by {@link HashSet}s.
 * @author Kristin Behrens, Robin Gawenda, Swen Jung
 * @param <E>
 */
public class HashList<E> implements List<E>
{
	ArrayList<E> list;
	HashSet<E> set;
	/**
	 * 
	 */
	private static final long serialVersionUID=7273807835432839256L;

	public HashList()	{
		list = new ArrayList<E>();
		set = new HashSet<E>();
	}
	
	public boolean add(E o) {
		if(contains(o)) 
			return false;
		else {
			set.add(o);
			list.add(o);
			return true;
		}
	}
	
	public E get(int index) {
		return list.get(index);
	}

	public int size() {
		return list.size();
	}

	public boolean contains(Object o) {
		if(set.contains(o))
			return true;
		else 
			return false;
	}
	
	public boolean remove(Object o) {
		if(set.contains(o)) {
			return list.remove(o)&&set.remove(o);
		} else
			return false;
	}

	public void add(int index, E element) {
		set.add(element);
		list.add(index, element);
	}

	public boolean addAll(Collection<? extends E> c) {
		return set.addAll(c)&&list.addAll(c);
	}

	public boolean addAll(int index, Collection<? extends E> c) {
		return set.addAll(c)&&list.addAll(index, c);
	}

	public void clear() {
		set.clear();
		list.clear();
	}

	public boolean containsAll(Collection<?> c) {
		return list.containsAll(c);
	}

	public int indexOf(Object o) {
		return list.indexOf(o);
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public Iterator<E> iterator() {
		return list.listIterator();
	}

	public int lastIndexOf(Object o) {
		return list.lastIndexOf(o);
	}

	public ListIterator<E> listIterator() {
		return list.listIterator();
	}

	public ListIterator<E> listIterator(int index) {
		return list.listIterator(index);
	}

	public E remove(int index) {
		E obj = list.remove(index);
		set.remove(obj);
		return obj;
	}

	public boolean removeAll(Collection<?> c) {
		return list.removeAll(c)&&set.removeAll(c);
	}

	public boolean retainAll(Collection<?> c) {
		return list.retainAll(c)&&set.retainAll(c);
	}

	public E set(int index, E element) {
		E result=list.set(index, element);
		set.remove(result);
		set.add(result);
		return result;
	}

	public List<E> subList(int fromIndex, int toIndex) {
		return list.subList(fromIndex, toIndex);
	}

	public Object[] toArray() {
		return list.toArray();
	}

	public <T> T[] toArray(T[] a) {
		return list.toArray(a);
	}
}
